<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_grid_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/grids/_ci/ci_attachment_select/ci_attachment_select_grid_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_composite_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_attachment_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_mod_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class CI_Attachment_Select_WP extends TCMS_Admin_Page_W_Grid_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_ci/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('category');
		$this->add_transit_var('ci');
		$this->add_transit_var('vpage');
		$this->add_transit_var('ret');
		$this->add_transit_var('vpt_context');
	}


	protected function create_grid_ctrl() {
		$tvm = $this->get_tvm();

		$context = $this->get__context();
		if (!$context->exists_in_get('ci')) {
			throw new Tangra_Module_Exception('Expecting ci parameter.');
		}

		if (!$tvm->is_page_var_registered('ci_attachment_select', 'filename')) {
			$tvm->register_page_var('ci_attachment_select', 'filename');
			$tvm->register_page_var('ci_attachment_select', 'note');
		}
		$this->process_filter($tvm);


		$ret = new CI_Attachment_Select_Grid_Ctrl('ci_attachment_select', $tvm, Tangra_Parameter_Method::GET, $context->get_from_get('ci'));


		return $ret;
	}


	protected function default_event($page_num = NULL) {
		$this->export('vpt_context', $this->get_transit_var('vpt_context', true));

		$dbc = $this->get_dbc();

		$context = $this->get__context();
		if ($context->exists_in_get('ci')) {
			$ci_id = $context->get_from_get('ci');
			$this->export('ci_id', $ci_id);
			$this->set_transit_var_value('ci', $ci_id);
			$ci = new TCMS_CI_Composite_DBC($this->_config->get_hidden_dir().'inc/');
			if (!$ci->load_by_id($dbc, $ci_id)) {
				throw new Tangra_Module_Exception('Cannot load attachments for this ci item because ci item does not exists. ID = '.$ci_id);
			}
		} else {
			throw new Tangra_Module_Exception('Expecting ci parameter.');
		}

		$this->export('ci_title', $ci->get_as_short_plain_text());


		if ($context->exists_in_get('select')) {
			$attachment_id = $context->get_from_get('select');
			$dbc = $this->get_dbc();
			if (!TCMS_ci_Attachment_DBC::exists($dbc, $ci_id, $attachment_id)) {
				$ci_attachment = new TCMS_ci_Attachment_DBC();
				$ci_attachment->set_ci($ci_id);
				$ci_attachment->set_attachment($attachment_id);
				try {
					$dbc->start_trans();
					$ci_attachment->save($dbc);
					$this->create_ci_mod($dbc, $ci_id);
					$dbc->complete_trans();
				} catch(Exception $e) {
					$dbc->fail_trans();
					$dbc->complete_trans();
					throw $e;
				}
			}

		} elseif ($context->exists_in_get('deselect')) {
			$ci_attachment_id = $context->get_from_get('deselect');
			$dbc = $this->get_dbc();
			try {
				$dbc->start_trans();
				TCMS_ci_Attachment_DBC::delete($dbc, $ci_attachment_id);
				$this->create_ci_mod($dbc, $ci_id);
				$dbc->complete_trans();
			} catch(Exception $e) {
				$dbc->fail_trans();
				$dbc->complete_trans();
				throw $e;
			}
		}


		$ret = $this->get_transit_var('ret', true);
		if ($ret) {
			$this->export('ret', $ret);
		}

		$vpage_id = $this->get_transit_var('vpage', true);
		if ($vpage_id) {
			$vpage = new TCMS_VPage_DBC();
			if ($vpage->load_by_id($dbc, $vpage_id)) {
				$this->export('vpage', $vpage);
			}
		}

		return parent::default_event($page_num);
	}


	private function create_ci_mod(DB_Connection $dbc, $ci_id) {
		$ci_mod = new TCMS_CI_Mod_DBC();
		$ci_mod->set_ci($ci_id);
		$ci_mod->set_modified_by($this->get_user()->get_id());
		$ci_mod->save($dbc);
	}


	private function process_filter($vm) {
		$context = $this->get__context();
		if ($context->exists_in_get('filename')) {
			$filename = $context->get_from_get('filename');
			$vm->set_page_var('ci_attachment_select', 'filename', $filename);
		}
		$filename = $vm->get_page_var('ci_attachment_select', 'filename');
		$this->export('filename', $filename);


		if ($context->exists_in_get('note')) {
			$note = $context->get_from_get('note');
			$vm->set_page_var('ci_attachment_select', 'note', $note);
		}
		$note = $vm->get_page_var('ci_attachment_select', 'note');
		$this->export('note', $note);
	}
}